﻿/* Web 扩展类
 */

/*待测试*/
namespace LSLib.Web
{
    using LSLib.Net;
    using Newtonsoft.Json;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Net;
    using System.Text;
    using System.Web;

    public class WebEx
    {
        public class Par
        {
            public string Param { get; set; }
            public string Val { get; set; }
        }

        public class SubmitResult
        {
            public bool result { get; set; }
        }

        public static void Post(string url, params Par[] param)
        {
            if (InternetHelper.IsConnectInternet() == false) throw new Exception("当前网络环境，无法连接到互联网！");

            Encoding myEncoding = Encoding.UTF8;
            StringBuilder sbParam = new StringBuilder();
            foreach (Par next in param)
            {
                if (param.GetValue(0) != next) sbParam.Append("&");
                sbParam.Append(HttpUtility.UrlEncode(next.Param, myEncoding));
                sbParam.Append("=");
                sbParam.Append(HttpUtility.UrlEncode(next.Val, myEncoding));
            }

            byte[] postBytes = Encoding.ASCII.GetBytes(sbParam.ToString());
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=gb2312";
            request.ContentLength = postBytes.Length;
            using (Stream reqStream = request.GetRequestStream())
            {
                reqStream.Write(postBytes, 0, postBytes.Length);
            }

            bool postResult = false;
            using (WebResponse response = request.GetResponse())
            {
                List<byte> lst = new List<byte>();
                using (Stream ss = response.GetResponseStream())
                {
                    int oneByte;
                    while (ss.CanRead)
                    {
                        oneByte = ss.ReadByte();
                        if (oneByte == -1) break;
                        lst.Add((byte)oneByte);
                    }
                }

                SubmitResult submitResult = GetObject<SubmitResult>(lst.ToArray());
                postResult = submitResult.result;
            }

            if (!postResult) throw new Exception("提交失败：" + url);
        }

        public static T Get<T>(string url) where T : class
        {
            if (InternetHelper.IsConnectInternet() == false) throw new Exception("当前网络环境，无法连接到互联网！");

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            List<byte> lst = new List<byte>();
            using (Stream stream = response.GetResponseStream())
            {
                while (stream.CanRead)
                {
                    int oneByte = stream.ReadByte();
                    if (oneByte == -1) break;
                    lst.Add((byte)oneByte);
                }
            }
            return GetObject<T>(lst.ToArray());
        }

        private static T GetObject<T>(byte[] jsonTextBytes) where T : class
        {
            string jsonText = System.Text.Encoding.UTF8.GetString(jsonTextBytes);
            return GetObject<T>(jsonText);
        }
        private static T GetObject<T>(string jsonText) where T : class
        {
            //需要引用：Newtonsoft.Json.dll
            return JavaScriptConvert.DeserializeObject<T>(jsonText);
        }
    }
}
